package locations.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import locations.model.TipoProblematica;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import locations.dao.implement.ProblematicTypeDAOHibernateJPA;
import locations.dao.implement.UserDAOHibernateJPA;
import locations.model.Usuario;

public class SaveUserAction extends ActionSupport 
        implements ModelDriven<Usuario>, ServletRequestAware, ServletResponseAware
{

    private static final long serialVersionUID = -8124967730231198191L;
	
    private HttpServletRequest request; 
    private HttpServletResponse response; 
    private Usuario user = new Usuario();

    public String execute()
    {
        UserDAOHibernateJPA dao = new UserDAOHibernateJPA();
        dao.save(this.getUser());
        return SUCCESS;
    }


    @Override
    public Usuario getModel()
    {
        return this.user;
    }

    public HttpServletRequest getRequest()
    {
        return request;
    }

    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    public void setResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public HttpServletResponse getResponse()
    {
        return this.response;
    }

    @Override
    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    @Override
    public void setServletRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    public void validate()
    {
        boolean ok = true;
        String usuario = this.getUser().getUsuario();
        String contrasena = this.getUser().getContrasena();
        String rol = this.getUser().getRol();
        if (usuario.equals(""))
        {
            addFieldError("usuario", "El usuario no puede estar en blanco");
                ok = false;
        }
        if (contrasena.equals(""))
        {
            addFieldError("contrasena", "La contraseña no puede estar en blanco");
                ok = false;
        }
        if (rol.equals(""))
        {
            addFieldError("rol", "El rol no puede estar en blanco");
                ok = false;
        }
        if (ok)
        {
            UserDAOHibernateJPA dao = new UserDAOHibernateJPA();
            long auxId = (this.getUser().getId() == null ? 0 : this.getUser().getId());
            if (dao.exists(usuario, auxId))
            {
                addFieldError("usuario", "Ya existe un usuario con ese nombre");
                ok = false;
            }
        }
    }

    public Usuario getUser() {
        return user;
    }

    public void setUser(Usuario user) {
        this.user = user;
    }
    
}
